/**
 * Copyright (c) 2008, Keisuke Inoue, Syracuse Unviersity
 * All rights reserved.
 *
 */
package edu.syr.iis.web.model;

import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;

import edu.syr.iis.web.util.HibernateUtil;

/**
 * @author kinoue
 *
 */
public class UserManager
{
	/**
	 * Returns list of all User database records sorted by userId
	 */
	public List<User> getUsers()
	{
		List<User> list = null;
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();

		try
		{
			list = session.createQuery("FROM User ORDER BY UserId").list();

			for (User user: list)
			{
				Hibernate.initialize(user.getAssignments());
				Hibernate.initialize(user.getAnnotationSessions());
			//	Hibernate.initialize(user.getAssignedConversations());
			}
			session.getTransaction().commit();
			
		}
		catch (HibernateException e)
		{
			session.getTransaction().rollback();
			throw e;
		}
		return list;
	}

	public User checkUser(String name, String password)
	{
		User user = this.getUser(name);		
		if (user != null && user.getPassword().equals(password))
			return user;
		else
			return null;					
	}

		
	public User getUser(String name)
	{
		User user = null;
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();

		try
		{
			user = (User) session.createQuery(
					"FROM User " +
			" WHERE userName = ?").setString(0, name).uniqueResult();
			Hibernate.initialize(user.getAssignments());
			Hibernate.initialize(user.getAnnotationSessions());
			// Hibernate.initialize(user.getAssignedConversations());
			session.getTransaction().commit();
		}
		catch (HibernateException e)
		{
			session.getTransaction().rollback();
			throw e;
		}
		return user;
	}
	
	public User getUser(int UserId)
	{
		User user = null;
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();

		try
		{
			user = (User) session.createQuery(
					"FROM User " +
			" WHERE UserId = ?").setInteger(0, UserId).uniqueResult();
			Hibernate.initialize(user.getAnnotationSessions());
			Hibernate.initialize(user.getAssignedConversations());
			session.getTransaction().commit();
		}
		catch (HibernateException e)
		{
			session.getTransaction().rollback();
			throw e;
		}	
		return user;
	}

	/**
	 * Saves a User object.
	 */
	public void saveUser(User User)
	{
		Session session = HibernateUtil.getSessionFactory()
		.getCurrentSession();
		session.beginTransaction();
		try
		{
			session.saveOrUpdate(User);
			session.getTransaction().commit();
		}
		catch (HibernateException e)
		{
			session.getTransaction().rollback();
			throw e;
		}
	}

	/**
	 * Deletes User record with matching UserId 
	 */
	public void deleteUser(int UserId)
	{
		Session session = 
			HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();	
		try
		{
			Object toDelete = session.get(User.class,new Integer(UserId));

			if (toDelete != null) { 
				session.delete(toDelete);
			}

			session.flush();
			session.getTransaction().commit();
		}
		catch (HibernateException e)
		{
			session.getTransaction().rollback();
			throw e;
		}
	}
}
